package com.unsiv.helicopter.entity;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.entity.Entity;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.widget.Toast;

import com.unsiv.helicopter.Command;
import com.unsiv.helicopter.entity.AdjustmentLeft.OnLeftClickListener;
import com.unsiv.helicopter.entity.AdjustmentRight.OnRightClickListener;

/**
 * <p>
 * 微调控件
 * </p>
 * 
 * @author fanzhen
 * @time 11:25:02 AM Nov 1, 2011
 * @email fanzhen.meng@gmail.com
 * @qq 474146502
 */
public class Adjustment extends Entity implements OnLeftClickListener,
		OnRightClickListener {
	private Context context;
	public AdjustmentLeft left;
	public AdjustmentRight right;
	private ChangeableText valueText;
	private static int valueInt = 0;
	public static int status = 0;// -1左边 ，1右边

	public Adjustment(Context context, final int pX, final int pY,
			final TiledTextureRegion leftTextureRegion,
			final TiledTextureRegion rightTextureRegion, Font mFont) {
		super(0, 0);
		this.context = context;
		this.left = new AdjustmentLeft(pX, pY, leftTextureRegion);
		this.left.setOnLeftClickListener(this);
		this.valueText = new ChangeableText(pX + 100, pY, mFont, "0",
				"XXXX".length());
		this.valueText.setBlendFunction(GL10.GL_SRC_ALPHA,
				GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.valueText.setAlpha(0.5f);
		this.valueText.setText("00");
		this.right = new AdjustmentRight(pX + 180, pY, rightTextureRegion);
		this.right.setOnRightClickListener(this);
		this.attachChild(this.left);
		this.attachChild(this.valueText);
		this.attachChild(this.right);
		Command.mValueOfRf = 0;
	}

	@Override
	public void onLeftClick() {
		if(!Command.needDoCommand){
			Toast.makeText(context, "Sorry please insert a handset.",
					Toast.LENGTH_SHORT).show();
			return;
		}
		AudioManager manager = (AudioManager) context
				.getSystemService(Activity.AUDIO_SERVICE);
		int maxv1 = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		int volume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
		if (maxv1 != volume) {
			Toast.makeText(context, "Sorry your device is not in max volume.",
					Toast.LENGTH_SHORT).show();
			return;
		}
		if (!Command.allowCommand) {
			Toast.makeText(
					context,
					"Sorry please turn on the swither.",
					Toast.LENGTH_SHORT).show();
			return;
		}
		if (status == 0) {
			Command.mRectification = 0;
			this.left.changeBright();
			valueInt++;
			status = -1;
		} else if (status == -1 && valueInt <= 30) {
			valueInt++;
		} else if (status == 1) {
			valueInt--;
			if (valueInt == 0) {
				status = 0;
				this.right.changeBlack();
			}
		}
		String tmpStr = "00";
		if (valueInt < 10) {
			tmpStr = "0" + String.valueOf(valueInt);
		} else {
			tmpStr = String.valueOf(valueInt);
		}
		valueText.setText(tmpStr);
		Command.mValueOfRf = valueInt;
	}

	@Override
	public void onRightClick() {
		if(!Command.needDoCommand){
			Toast.makeText(context, "Sorry please insert a handset.",
					Toast.LENGTH_SHORT).show();
			return;
		}
		AudioManager manager = (AudioManager) context
				.getSystemService(Activity.AUDIO_SERVICE);
		int maxv1 = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		int volume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
		if (maxv1 != volume) {
			Toast.makeText(context, "Sorry your device is not in max volume.",
					Toast.LENGTH_SHORT).show();
			return;
		}
		if (!Command.allowCommand) {
			Toast.makeText(
					context,
					"Sorry please turn on the swither.",
					Toast.LENGTH_SHORT).show();
			return;
		}
		if (status == 0) {
			Command.mRectification = 1;
			this.right.changeBright();
			status = 1;
			valueInt++;
		} else if (status == -1) {
			valueInt--;
			if (valueInt == 0) {
				status = 0;
				this.left.changeBlack();
			}
		} else if (status == 1 && valueInt <= 30) {
			valueInt++;
		}
		String tmpStr = "00";
		if (valueInt < 10) {
			tmpStr = "0" + String.valueOf(valueInt);
		} else {
			tmpStr = String.valueOf(valueInt);
		}
		valueText.setText(tmpStr);
		Command.mValueOfRf = valueInt;
	}
}